const router = require('express').Router()
const Search = require('../../gitlab/search')

// 校验method参数
router.param('kind', (req, res, next, kind) => {
  const supported = ['assemble', 'issues', 'notes']
  if (!supported.includes(kind)) {
    next(new Error(`不支持按类型: ${kind} 查询. 请替换成 ${supported.toString()}`))
  } else {
    req.kind = kind
    next()
  }
})

router.get('/search/:kind', (req, res) => {
  const { kind } = req.params
  const { page, limit, search } = req.query
  const instance = new Search(search, page, limit)
  instance[kind]().then(data => {
    res.send(data)
  })
})

module.exports = router
